package cn.iocoder.yudao.module.crossborder.order.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.crossborder.order.api.dto.OrderCreateReqDTO;
import cn.iocoder.yudao.module.crossborder.order.api.dto.OrderDTO;
import cn.iocoder.yudao.module.crossborder.order.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 订单管理
 *
 * @author LHL
 */
@Tag(name = "管理后台 - 订单管理")
@RestController
@RequestMapping("/crossborder/order")
@Validated
public class OrderController {

    @Resource
    private OrderService orderService;

    @GetMapping("/get")
    @Operation(summary = "获得订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:order:query')")
    public CommonResult<OrderDTO> getOrder(@RequestParam("id") Long id) {
        OrderDTO order = orderService.getOrder(id);
        return success(order);
    }

    @GetMapping("/list")
    @Operation(summary = "获得订单列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('crossborder:order:query')")
    public CommonResult<List<OrderDTO>> getOrderList(@RequestParam("ids") List<Long> ids) {
        List<OrderDTO> orders = orderService.getOrderList(ids);
        return success(orders);
    }

    @PostMapping("/create")
    @Operation(summary = "创建订单")
    @PreAuthorize("@ss.hasPermission('crossborder:order:create')")
    public CommonResult<Long> createOrder(@RequestBody @Valid OrderCreateReqDTO createReq) {
        Long id = orderService.createOrder(createReq);
        return success(id);
    }

    @PostMapping("/cancel")
    @Operation(summary = "取消订单")
    @Parameter(name = "orderId", description = "订单编号", required = true, example = "1024")
    @Parameter(name = "reason", description = "取消原因", required = true, example = "用户主动取消")
    @PreAuthorize("@ss.hasPermission('crossborder:order:cancel')")
    public CommonResult<Boolean> cancelOrder(@RequestParam("orderId") Long orderId,
                                            @RequestParam("reason") String reason) {
        Boolean result = orderService.cancelOrder(orderId, reason);
        return success(result);
    }

    @PostMapping("/confirm")
    @Operation(summary = "确认订单")
    @Parameter(name = "orderId", description = "订单编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:order:confirm')")
    public CommonResult<Boolean> confirmOrder(@RequestParam("orderId") Long orderId) {
        Boolean result = orderService.confirmOrder(orderId);
        return success(result);
    }

    @PostMapping("/pay")
    @Operation(summary = "支付订单")
    @Parameter(name = "orderId", description = "订单编号", required = true, example = "1024")
    @Parameter(name = "paymentMethod", description = "支付方式", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('crossborder:order:pay')")
    public CommonResult<Boolean> payOrder(@RequestParam("orderId") Long orderId,
                                         @RequestParam("paymentMethod") Integer paymentMethod) {
        Boolean result = orderService.payOrder(orderId, paymentMethod);
        return success(result);
    }

    @PostMapping("/fulfill")
    @Operation(summary = "履约订单")
    @Parameter(name = "orderId", description = "订单编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:order:fulfill')")
    public CommonResult<Boolean> fulfillOrder(@RequestParam("orderId") Long orderId) {
        Boolean result = orderService.fulfillOrder(orderId);
        return success(result);
    }

    @PostMapping("/complete")
    @Operation(summary = "完成订单")
    @Parameter(name = "orderId", description = "订单编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:order:complete')")
    public CommonResult<Boolean> completeOrder(@RequestParam("orderId") Long orderId) {
        Boolean result = orderService.completeOrder(orderId);
        return success(result);
    }

}
